
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <netinet/if_ether.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>

// int sk = socket(PF_PACKET, SOCK_RAW, htons(0x88CC));/*创建原始套接字*/
// https://www.xuebuyuan.com/1764043.html

// int socket(int domain, int type, int protocol);
static void test_socket_all()
{
    u_int8_t buf[1024];
    int flag = 0;

    // PF_PACKET
    struct sockaddr_ll sll;
    memset(&sll, 0, sizeof(sll));

    sll.sll_family = PF_PACKET;
    // /*sll虽然是用来指定目的地址的，但是在这个结构体中sll_ifindex 却指定的是本机发送报文的接口索引*/
    // sll.sll_ifindex = if_nametoindex("eth0");
    sll.sll_ifindex = if_nametoindex("enp0s20f0u2");
    // sll.sll_protocol = htons(0x88CC);
    // sll.sll_protocol = htons(ETH_P_IP);
    sll.sll_protocol = htons(ETH_P_ALL);
    // sll.sll_protocol = htons(0x80e0);
    // sll.sll_addr[0] = 0x34; /*sll_addr指定目的MAC地址*/
    // sll.sll_addr[1] = 0xb0;
    // sll.sll_addr[2] = 0x52;
    // sll.sll_addr[3] = 0xda;
    // sll.sll_addr[4] = 0xda;
    // sll.sll_addr[5] = 0x18;
    socklen_t len = sizeof(sll);

    int fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    // int fd = socket(PF_PACKET, SOCK_RAW, htons(0x80e0));
    // int fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
    // int fd = socket(PF_PACKET, SOCK_DGRAM, htons(0x80e0));

    perror("????");
    // sendto(sk, lpacket, 14, 0, (struct sockaddr*)&sll, sizeof(struct sockaddr_ll));
    while (fd >= 0) {
        int res = recvfrom(fd, buf, 100, flag, (struct sockaddr*)&sll, &len);
        printf("type: %02x%02x\n", buf[12], buf[13]);
        // int res = recvfrom(fd, buf, 100, flag, NULL, NULL);
        // printf("read data len: %d\n", res);
        // for (int i = 0; i < res; i++) {
        //     printf("%c", buf[i]);
        // }
    }
}

int main(int argc, char const* argv[])
{
    /* code */
    test_socket_all();
    return 0;
}
